var appInstance = getApp();
Page({
	data:{
		selectedKItemData:null,//项目选择k
		selectedVItemData:null,//项目选择v

		itemTitleInfo:null,//标题项配置
		itemListData:null,//选择列表信息
		isLoadingBtn: false,

		apiData:null,//api接口列表
		pId:null,//产品信息
		pImage:null,//产品图片
		pTitle:null,//产品标题
		pItems:null//产品选择项
	},
	
	onLoad: function(options){


		/*初始化接口*/
		var that = this;
      	that.apiData = appInstance.globalData.apiData;
		var argPid = options.pId;
		

		/*获取商品信息及选择项目*/
		//var url = that.apiData.initPItems+'&pid='+argPid;
		var resInfo = initPItems(argPid);

		var pInfo = resInfo.pInfo;//产品信息
        var itemTitleInfo = resInfo.itemTitleInfo;//标题项目信息
        var itemListData =resInfo.itemListData;//列表选择
        var pItems = resInfo.itemData;//产品下项目列表
        var selectedKItemData = resInfo.defaultKItemData;//选择的k
        var selectedVItemData = resInfo.defaultVItemData;//选择的v
        that.setData({
        	pId: argPid,
        	pTitle: pInfo.title,
        	pImage: pInfo.image,
        	pItems: pItems,
        	itemTitleInfo: itemTitleInfo,
        	itemListData: itemListData,
        	selectedKItemData: selectedKItemData,
        	selectedVItemData: selectedVItemData
        })
        wx.setNavigationBarTitle({
        	title: that.data.pTitle
        })
		// wx.request({
		// 	url:url,
		// 	success: function(res){

		// 		var resInfo = res.data;

		// 		if(resInfo.code!='0000'){

	 //                wx.showModal({
	 //                  title: '提示',
	 //                  content: '数据获取失败',
	 //                  showCancel: false
	 //                })
	 //                return false;
		//         }

		//         var pInfo = resInfo.pInfo;//产品信息
		//         var itemTitleInfo = resInfo.itemTitleInfo;//标题项目信息
		//         var itemListData =resInfo.itemListData;//列表选择
		//         var pItems = resInfo.itemData;//产品下项目列表
		//         var selectedKItemData = resInfo.defaultKItemData;//选择的k
		//         var selectedVItemData = resInfo.defaultVItemData;//选择的v
		//         that.setData({
		//         	pId: argPid,
		//         	pTitle: pInfo.title,
		//         	pImage: pInfo.image,
		//         	pItems: pItems,
		//         	itemTitleInfo: itemTitleInfo,
		//         	itemListData:itemListData,
		//         	selectedKItemData: selectedKItemData,
		//         	selectedVItemData: selectedVItemData
		//         })
		//         wx.setNavigationBarTitle({
		//         	title: that.data.pTitle,
		//         	image: that.data.pImage
		//         })
		// 	}
		// })
	},

	bindSelectItem: function(e){

		var selectedId = e.detail.value;
		var selectKItem = e.currentTarget.dataset.kitem;


		var selectedKItemData  = this.data.selectedKItemData;
		var selectedVItemData  = this.data.selectedVItemData;

		selectedKItemData[selectKItem] = selectedId;
		selectedVItemData[selectKItem] = this.data.pItems[selectKItem]['vdata'][selectedId];
		this.setData({
			selectedKItemData:selectedKItemData,
			selectedVItemData: selectedVItemData
		});
	},

	tapBtn: function(e){

		var that = this;
		var btnStatus = this.data.isLoadingBtn;
		var pid = e.currentTarget.dataset.pid;//产品ID

		var paramList = this.data.selectedKItemData;
		var urls = 'pid='+pid;
		for (var key in paramList) {

			urls += '&'+key + '=' + paramList[key];
		}

		wx.showToast({
			title: '价格计算中，请稍后...',
			icon: 'loading',
			duration:1000,
			mask: true
		});

		//urls = urls.substring(0, urls.lastIndexOf('&'));
		var jumpUrl = "../price/price?"+urls;
		wx.navigateTo({
			url:jumpUrl
		});

		// wx.request({
		// 	url: 'https://testapp.cailebang.com/calculatePrize.php',
		// 	data: objData,
		// 	header: {
		// 		'content-type' : 'application/json'
		// 	},
		// 	success: function(res){
				
		// 		wx.hideLoading();

		// 		wx.showModal({
		// 			title:'提示框',
		// 			content:'乱七八杂的内容。。。',
		// 			showCancel: true

		// 		});
		
		// 	}
		// });
	}
})


function getObjKeys(obj){

	var returnKeys = [];
	var i = 0;
	for(var key in obj){
		returnKeys[i++] = key;
	}
	return returnKeys;
}

function getObjValues(obj){

	var returnValues = [];
	var i = 0;
	for(var key in obj){
		returnValues[i++] = obj[key];
	}
	return returnValues;
}



/*初始化产品参数*/
function initPItems(pid){
	var typeConf = appInstance.globalData.typeConf[pid];
	var itemConf = appInstance.globalData.itemsConf[pid];
	var itemsDetailConf = appInstance.globalData.itemsDetailConf;
	var pInfo = typeConf;

	var itemData = {};//所有列表
	var defaultKItemInfo = {};//默认的key列表
	var defaultVItemInfo = {};//默认的value列表
	var itemTitleInfo = {};//标题项列表
	var itemListData = {};
	for(var itemKey in itemConf){

		var itemCn = itemConf[itemKey];
		var newKey = pid+'.'+ itemKey;

		if(!itemsDetailConf[newKey]){

			continue;
		}
		var item_keys_list = getObjKeys(itemsDetailConf[newKey]);
		var item_values_list = getObjValues(itemsDetailConf[newKey]);
		itemData[itemKey] = {
				data: itemsDetailConf[newKey],
				kdata: item_keys_list,
				vdata: item_values_list
		};

		defaultKItemInfo[itemKey] = 0;
		defaultVItemInfo[itemKey] = itemsDetailConf[newKey][item_keys_list[0]];
		itemTitleInfo[itemKey] = itemCn;
		itemListData[itemKey] = item_values_list;
	}
	return {
			code: '0000',
			pInfo: pInfo,
			itemTitleInfo: itemTitleInfo,
			itemListData: itemListData,
			itemData: itemData,
			defaultKItemData: defaultKItemInfo,
			defaultVItemData: defaultVItemInfo
		};
}